% Define column in Data_EarningsCall 
p.gvkey=1; p.rdate=2; p.dates_m=3; p.PRisk=4; p.firmid=5; p.EPU = 6;

% Data PRisk Innovation
p.PRisk_Inno=7; Data_EarningsCall(:,p.PRisk_Inno) = nan;
Data = sortrows(Data_EarningsCall,p.firmid);
firmid = unique(Data(:,p.firmid));
Count = 1;
for i = 1:size(firmid,1)
   D = Data(find(Data(:,p.firmid)==firmid(i)),[p.PRisk p.EPU]);
   s = size(D,1);
   if s>=12 % I require at least 12 data points to perform the regression to get the residual
    b = regress(D(2:end,1),[ones(size(D(2:end,1))) D(1:end-1,1) D(2:end,2)]); warning ('off');
    e = D(2:end,1) - b(1) -b(2)*D(1:end-1,1) - b(3)*(D(2:end,2));
    e = [nan;e];
    Data(Count:Count+s-1,p.PRisk_Inno)=e;
   else
    Data(Count:Count+s-1,p.PRisk_Inno)=nan;
   end
   Count = Count+s;
end
Data = sortrows(Data,p.gvkey);
clear Count D s b e e gvkey i

% Stock_PRisk_Inno
minDate_m = min(dates_m);
shiftedDates_m = dates_m - minDate_m + 1;
dates_LT_m = zeros( max(shiftedDates_m), 1 );
dates_LT_m(shiftedDates_m) = 1 : numel( shiftedDates_m ) ;
Stock_PRisk_Inno = nan( numel(dates_m), numel(firmid) ) ;

for pIm = 1 : numel(firmid)
    lIm = Data(:,p.firmid) == firmid(pIm) ;   
    currentDates_m = Data(lIm,p.dates_m) ;  
    dIm = dates_LT_m(currentDates_m - minDate_m + 1) ;
    Stock_PRisk_Inno(dIm,pIm) = Data(lIm,p.PRisk_Inno) ;
end

clear dates_LT_m pIm lIm dIm minDate_m shiftedDates_m currentDates_m p

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C = [nan(1,size(call_deltahedge_returns_rb,2)); call_deltahedge_returns_rb];
P = [nan(1,size(put_deltahedge_returns_rb,2)); put_deltahedge_returns_rb]; 

call_PRisk_Before = nan(size(Stock_PRisk_Inno));
put_PRisk_Before = nan(size(Stock_PRisk_Inno)); 

call_PRisk_After = nan(size(Stock_PRisk_Inno));
put_PRisk_After = nan(size(Stock_PRisk_Inno)); 

for i = 3:size(C,1)-3
   for j = 1:size(C,2)
      if ~isnan(Stock_PRisk_Inno(i,j))
         call_PRisk_Before(i,j) = mean([C(i-2:i-1,j)]);
         call_PRisk_After(i,j) = mean([C(i+1:i+2,j)]);
         
         put_PRisk_Before(i,j) = mean([P(i-2:i-1,j)]);
         put_PRisk_After(i,j) = mean([P(i+1:i+2,j)]);        
      end       
   end
end
clear i j C P 

% Result
M = Stock_PRisk_Inno;
X1 = call_PRisk_Before*100;
X2 = call_PRisk_After*100;
X3 = put_PRisk_Before*100;
X4 = put_PRisk_After*100;

D = [reshape((repmat(firmid, [1, size(dates_m,1)]))', [size(firmid,1)*size(dates_m,1), 1]), ...
     repmat(dates_m, [size(X1,2) 1]), ...
     reshape(M,[size(M,1)*size(M,2) 1]), ...
     reshape(X1,[size(X1,1)*size(X1,2) 1]), ...
     reshape(X2,[size(X1,1)*size(X1,2) 1]), ...
     reshape(X3,[size(X1,1)*size(X1,2) 1]), ...
     reshape(X4,[size(X1,1)*size(X1,2) 1])];
clear M X1 X2 X3 X4

D(find(isnan(sum(D,2))),:) = [];

%
D_Increase = D(find(D(:,3)>prctile(D(:,3),67)),:);
D_Decrease = D(find(D(:,3)<prctile(D(:,3),33)),:);

Result_Event = nan(4,6);
% Results for call option returns
Result_Event(1,1) = mean(D_Increase(:,4));
Result_Event(2,1) = mean(D_Increase(:,5));
Result_Event(3,1) = mean(D_Increase(:,5)-D_Increase(:,4));
    stat = nwest(D_Increase(:,5)-D_Increase(:,4),[ones(size(D_Increase,1),1)],3); 
Result_Event(4,1) = stat.tstat(1);

Result_Event(1,2) = mean(D_Decrease(:,4));
Result_Event(2,2) = mean(D_Decrease(:,5));
Result_Event(3,2) = mean(D_Decrease(:,5)-D_Decrease(:,4));
    stat = nwest(D_Decrease(:,5)-D_Decrease(:,4),[ones(size(D_Increase,1),1)],3); 
Result_Event(4,2) = stat.tstat(1);

Result_Event(1,3) = mean(D_Increase(:,4)) - mean(D_Decrease(:,4));
Result_Event(2,3) = mean(D_Increase(:,5)) - mean(D_Decrease(:,5));
Result_Event(3,3) = mean((D_Increase(:,5)-D_Increase(:,4)) - (D_Decrease(:,5)-D_Decrease(:,4)));
    stat = nwest((D_Increase(:,5)-D_Increase(:,4)) - (D_Decrease(:,5)-D_Decrease(:,4)), ...
            [ones(size(D_Increase,1),1)],3);
Result_Event(4,3) = stat.tstat(1);

% Results for put option returns
Result_Event(1,4) = mean(D_Increase(:,6));
Result_Event(2,4) = mean(D_Increase(:,7));
Result_Event(3,4) = mean(D_Increase(:,7)-D_Increase(:,6));
    stat = nwest(D_Increase(:,7)-D_Increase(:,6),[ones(size(D_Increase,1),1)],3); 
Result_Event(4,4) = stat.tstat(1);

Result_Event(1,5) = mean(D_Decrease(:,6));
Result_Event(2,5) = mean(D_Decrease(:,7));
Result_Event(3,5) = mean(D_Decrease(:,7)-D_Decrease(:,6));
    stat = nwest(D_Decrease(:,7)-D_Decrease(:,6),[ones(size(D_Increase,1),1)],0); 
Result_Event(4,5) = stat.tstat(1);

Result_Event(1,6) = mean(D_Increase(:,6)) - mean(D_Decrease(:,6));
Result_Event(2,6) = mean(D_Increase(:,7)) - mean(D_Decrease(:,7));
Result_Event(3,6) = mean((D_Increase(:,7)-D_Increase(:,6)) - (D_Decrease(:,7)-D_Decrease(:,6)));
    stat = nwest((D_Increase(:,7)-D_Increase(:,6)) - (D_Decrease(:,7)-D_Decrease(:,6)), ...
            [ones(size(D_Increase,1),1)],3);
Result_Event(4,6) = stat.tstat(1);

clear D D_Increase D_Decrease stat call_PRisk_After call_PRisk_Before put_PRisk_After put_PRisk_Before 